package com.newlink.building.login.common

import android.content.Context
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.newlink.building.login.R
import com.newlink.building.common_base.ext.showToast

/**
 * @Author: Jake
 * @Date: 2022/10/26
 * @Description:
 */
object Module_Login_SpannableUtils {

    fun setSpannableStringView(
        context: Context,
        tv: TextView,
        content1: String,
        actionLink: Unit?,
        content2: String,
        actionLink2: Unit?,
        resId: Int
    ) {
        var str = context.getString(resId)
        val index1 = str.indexOf("#")
        val index2 = str.indexOf("*") + (content1.length - 4)
        str = str.replace("####", content1)
        str = str.replace("****", content2)
        val spannableString = SpannableStringBuilder(str)
        val linkedForground1 = ForegroundColorSpan(ContextCompat.getColor(context, R.color.yellow))
        val linkedForground2 = ForegroundColorSpan(ContextCompat.getColor(context, R.color.yellow))

        with(spannableString) {
            setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        showToast("USER LICENSE")
//                        mPresenter?.startBrowser(this@LoginActivity ,  Constant.USER_LICENSE_URL)
                        actionLink
                    }

                },
                index1,
                index1 + content1.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            setSpan(
                linkedForground1,
                index1,
                index1 + content1.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        showToast("PRIVACY LICENSE")
//                        mPresenter?.startBrowser(this@LoginActivity ,  Constant.PRIVACY_LICENSE_URL)
                        actionLink2
                    }

                },
                index2,
                index2 + content2.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            setSpan(
                linkedForground2,
                index2,
                index2 + content2.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
        }

        tv.text = spannableString
        tv.movementMethod = LinkMovementMethod.getInstance()
    }

}